/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.azet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import sk.yw.azetclient.Main;
import sk.yw.azetclient.connection.AbstractParser;
import sk.yw.azetclient.connection.PagesSaver;

/**
 *
 * @author error216
 */
public class AuthenticationParser extends AbstractParser<String> {
    
    private static final Logger logger = Logger.getLogger(AuthenticationParser.class);
    
    private static final Pattern I9_PATTERN = Pattern.compile(
            "<meta http-equiv=\"refresh\" content=\"0;url=.*?i9=(.*?)(&.*?)?\">");
    private static final String TRUE_KEYWORD = "Prebieha prihlasovanie.";
    private static final String FALSE_KEYWORD = "Nesprávne Azet ID alebo heslo";
    
    public String parse(InputStream in) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in,
                Main.getProperty("azetclient.azet.encoding")));
        PagesSaver saver = new PagesSaver("authentication");
        
        String result = "";
        boolean authenticated = false;
        boolean keywordFound = false;
        
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                if (result.isEmpty()) {
                    Matcher matcher = I9_PATTERN.matcher(line);
                    if (matcher.find()) {
                        result = matcher.group(1);
                        logger.debug("i9 found: " + matcher.group(1));
                    }
                }
                if (line.contains(TRUE_KEYWORD)) {
                    keywordFound = true;
                    authenticated = true;
                }
                if (line.contains(FALSE_KEYWORD)) {
                    keywordFound = true;
                    authenticated = false;
                }

                saver.println(line);
            }
        } finally {
            saver.close();
            reader.close();
        }
        
        if (!keywordFound) {
            logger.warn("Have found neither TRUE_KEYWORD nor FALSE_KEYWORD");
            result = "";
        }
        if (!authenticated) result="";
        return result;
    }

}
